/*
 * @(#)MessageFormatterFactory.java 
 * Copyright (c) 2006 DCIVision Ltd All rights reserved.
 * 
 *  This software is the confidential and proprietary information of DCIVision Ltd 
 *  ("Confidential Information"). You * shall not disclose such Confidential
 *   Information and shall use it only in accordance with the terms of the 
 *   license agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.alert.core;

import java.sql.Connection;

import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.alert.bean.UpdateAlertSystemLog;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;

/**
 * <p>
 * Class Name: MessageFormatterFactory.java
 * </p>
 * 
 * @author Greatwall shao
 * @company DCIVision Limited
 * @creation date 2006-11-05
 */
public class MessageFormatterFactory {
  static MessageFormatterFactory instance = null;

  private MessageFormatterFactory() {

  }

  public static MessageFormatterFactory getInstance() {
    instance = new MessageFormatterFactory();
    return instance;
  }

  synchronized public MessageFormatter getMessageFormatter(UpdateAlertSystemLog syslog,
      SessionContainer sessionContainer, Connection dbConn) throws ApplicationException {
    MessageFormatter messageFormatter = null;

    String objectType = syslog.getObjectType().trim();
    boolean isDms = (UpdateAlert.DOCUMENT_TYPE.equals(objectType))
        || (UpdateAlert.AUTOMATE_CHECKIN.equals(objectType))
        || (UpdateAlert.ISSUE_TYPE.equals(objectType));

    boolean isMeeting = (UpdateAlert.CALENDAR_TYPE.equals(objectType))
        || (UpdateAlert.CALENDAR_MODIFY_TYPE.equals(objectType))
        || (UpdateAlert.CALENDAR_INACTIVE_TYPE.equals(objectType));

    boolean isWorkflow = (UpdateAlert.WORKFLOW_TYPE.equals(objectType))
        || (UpdateAlert.DELEGATION_TYPE.equals(objectType));

    boolean isEmailRule = (UpdateAlert.EMAIL_ARCHIVE_TYPE.equals(objectType));
    
    String actionType = syslog.getActionType().trim();
    boolean isDocmentInvitation = isDms
                      &&(UpdateAlert.DOCUMENT_INVITATION_CANCEL.equals(actionType)
                        ||UpdateAlert.DOCUMENT_INVITATION_EXPIRED.equals(actionType)
                        ||UpdateAlert.DOCUMENT_INVITATION_UPDATE_DUEDATE.equals(actionType)
                        ||UpdateAlert.INVITE_ACTION.equals(actionType)
                        ||UpdateAlert.DISCUSS_ACTION.equals(actionType));

    if (isDms) {
      if (isDocmentInvitation) {
        messageFormatter = new DocumentInvitationMessageFormatter(syslog, sessionContainer, dbConn);

      }else{
      messageFormatter = new DocumentMessageFormatter(syslog, sessionContainer, dbConn);
      }
    }
    else if (isMeeting) {
      messageFormatter = new MeetingMessageFormatter(syslog, sessionContainer, dbConn);

    }
    else if (isWorkflow) {
      messageFormatter = new WorkflowMessageFormatter(syslog, sessionContainer, dbConn);

    }
    else if (isEmailRule) {
      messageFormatter = new EmailRuleMessageFormatter(syslog, sessionContainer, dbConn);

    }
    return messageFormatter;
  }
}
